import {
  SpreadModel
} from '../../../models/spread'
import {
  Format
}
from "../../../utils/common";
import {
  OrderModel
} from "../../../models/order";
const orderModel = new OrderModel()
import {
  MyModel
} from '../../../models/my'
const myModel = new MyModel()
import {
  AccountModel
} from '../../../models/account'
const accountmodel = new AccountModel()
const spreadModel = new SpreadModel()
const format = new Format()
const app = getApp()
let timerCountDown = {}
let timerDate = {}
Page({
  data: {
    canUse: my.canIUse('lifestyle'),
    countDownTime: {
      m: 0,
      s: 0,
      ms: 0
    },
    detail: {
      amount: 0,
      realAmount: 0,
      merchantSubsidyAmount: 0
    },
    cardAsn: '',
    openIfmemberpop: false, // 是否显示选择会员状态弹窗
    showConsume: false, // 是否显示选择会员状态弹窗-是否显示一键加油或者直接战舰
    firstClick: true,
    oilResultActive: '', // 活动链接及内容
  },
  onLoad(query) {
    let vm = this
    console.log(query)
    app.globalData.entityNum = ''
    // query.tradeNo = '2020040922001491071442297034'
    const time = format.date("yyyy年MM月dd日 hh:mm:ss", new Date())
    this.setData({
      time: time
    })
    this.loadDetail(query.yhorderID)
    this.getMainActiveParam()
    this.getCouponListByOrderIdOrOpenId(query.yhorderID)
    // this.showTime()
    this.data.cardAsn = app.globalData.user.cardAsn || ''
    console.log(this.data.cardAsn)
  },
  loadDetail(tradeNo) {
    let vm = this
    orderModel.loadDetail(tradeNo).then(res => {
      console.log('111111' + JSON.stringify(res))
      this.setData({
        detail: res,
      })
      if (res.payTypeNum && res.payTypeNum  == 3) {
        vm.loadMyAccount()
      }
    })
  },
  loadMyAccount() {
    myModel.loadMyAccount().then(res => {
      console.log(res)
      if (res) {
        if (res.code == '00') {
          this.setData({
            balance: res.result.balance
          })
        } else if (res.message) {
          this.setData({
            openTipPop: true,
            TipTxt: res.message,
            tips: 'close',
            balance: ''
          });
        }
      } else {
        this.setData({
          openTipPop: true,
          TipTxt: '获取账户信息失败',
          tips: 'close',
        });
      }
      
    })
  },
  getMainActiveParam() {
    myModel.getMainActiveParam(2).then(res => {
      // console.log(res)
      if (res) {
        if (res.imageSrc) {
          if (res.linkSrc) {
            this.setData({
              "oilResultActive[linkSrc]": res.linkSrc,
              "oilResultActive[imageSrc]": res.imageSrc,
              "oilResultActive[height]": res.height
            })
          } else {
            this.setData({
              "oilResultActive[imageSrc]": res.imageSrc,
              "oilResultActive[height]": res.height,
            })
          }
        }
      }
      
    })
  },
  //  获取充值送接口
  getCouponListByOrderIdOrOpenId (orderId) {
    let vm= this
    accountmodel.getCouponListByOrderIdOrOpenId('3',orderId).then(res => {
      console.log(res)
      if (res && res.data && res.data.length > 0) {
        vm.setData({
          consumeactive: res,
        })
      }
    })
  },
  toPage(e) {
    let vm = this
    const page = e.currentTarget.dataset.url
    if (this.data.firstClick) {
      this.data.firstClick = false
    } else {
      return
    }
    if (page == 'invoice') {
      my.alert({
        // title: '很抱歉',
        content: '功能上线中，敬请期待~',
        buttonText: '我知道了',
        success: () => {
          vm.data.firstClick = true
        }
      });
      return
    }
    if (page == 'oilResultActive' && this.data.oilResultActive.linkSrc) {
      if (this.data.oilResultActive.linkSrc == '/pages/recharge/recharge/recharge') {
        if (this.data.cardAsn) {
          my.redirectTo({
            url: '/pages/recharge/recharge/recharge?href=oilResult'
          })
        } else {
          this.setData({
            openIfmemberpop: true
          })
        }
      } else {
        my.redirectTo({
          url: this.data.oilResultActive.linkSrc
        })
      }
      return
    }
    my.redirectTo({
      url: page
    })
  },
  // 点击活动
  // toActive() {
  //   if (this.data.firstClick) {
  //     this.data.firstClick = false
  //   } else {
  //     return
  //   }
  //   if (this.data.cardAsn) {
  //     my.redirectTo({
  //       url: '/pages/recharge/recharge/recharge'
  //     })
  //   } else {
  //     this.setData({
  //       openIfmemberpop: true
  //     })
  //   }
  // },
  // 关掉是否会员弹窗
  onClose() {
    this.data.firstClick = true
    this.setData({
      openIfmemberpop: false,
    });
  },
  toMayi() {
    // my.navigateToMiniProgram({
    //   appId: '2019110168820582',
    //   path: 'pages/confirm/confirm',
    //   extraData:{
    //     "alipay_store_id":"2019110600077000000084416752"
    //   },
    //   success: (res) => {
    //     console.log(JSON.stringify(res))
    //   },
    //   fail: (res) => {
    //     console.log(JSON.stringify(res))
    //   }
    // });
    // return
    // my.reLaunch({
    //   url: '/pages/confirm/confirm'
    // })
    // app.backMayi()
  },
  attention() {
    const scene_id = app.globalData.station.myjy_source
    spreadModel.getLink(scene_id).then(res => {
      console.log(res)
    })
  },
  // showTime() {
  //   let downTime = 0
  //   timerCountDown = setInterval(() => {
  //     downTime += 6
  //     const countDownTime = format.watch(downTime)
  //     this.setData({
  //       countDownTime: countDownTime
  //     })
  //   }, 60);
  //   timerDate = setInterval(() => {
  //     const time = format.date("yyyy年MM月dd日 hh:mm:ss", new Date())
  //     this.setData({
  //       time: time
  //     })
  //   }, 1000);

  // },
  onHide() {
    // 页面隐藏
  },
  onUnload() {
    // 页面被关闭
    this.clearTimer()
  },
  clearTimer(){
    clearTimeout(timerCountDown)
    clearTimeout(timerDate)
  }
});